from __future__ import annotations

from game.config import COL_COUNT, ROW_COUNT, SCREEN_HEIGHT, SCREEN_WIDTH


class GridParser:
    """
    网格计算解析器
    """

    def __init__(self,row_count,col_count,start_x,start_y,end_x,end_y):
        self.row_count=row_count
        self.col_count=col_count

        self.min_x=min(start_x,end_x)
        self.min_y=min(start_y,end_y)

        self.max_x=max(start_x,end_x)
        self.max_y=max(start_y,end_y)

        wid_x=self.max_x-self.min_x
        hei_y=self.max_y-self.min_y
        

        self.cell_x=wid_x/col_count
        self.cell_y=hei_y/row_count
        pass
    

    def calc_xy_by_rc(self,r,c):
        # 根据rc计算单元格的中心坐标
        x=int(self.min_x+self.cell_x*c+self.cell_x/2)
        y=int(self.min_y+self.cell_y*(self.row_count-r-1)+self.cell_y/2)
        
        return (x,y)
    
    def calc_rc_by_xy(self,x,y):
        # 根据xy坐标计算rc的位置
        if x<self.min_x or x>self.max_x:
            return None
        if y<self.min_y or y>self.max_y:
            return None

        col=(x-self.min_x)//self.cell_x
        row=self.row_count-((y-self.min_y)//self.cell_y)-1

        return (int(row),int(col))

# print('import...')   
grid_parser=GridParser(ROW_COUNT,COL_COUNT,0,0,SCREEN_WIDTH,SCREEN_HEIGHT)